<?php

namespace app\console\controller;


use think\Cookie;
use think\Url;

use think\Request;

use think\Session;

use think\Controller;

use app\console\model\Manager as ManageModel;


class Login extends Controller

{

    protected $beforeActionList = [

        'checkLogin' => ['only' => 'index'],

    ];


    public function index()

    {

        if (Request::instance()->isPost()) {

            $ManageModel = new ManageModel();

            if ($ManageModel->login()) {
                if(session('role_id') == 4){
                    win('登录成功', Url::build('console/Salesman/my_team'));
                }
                if(session('role_id') == 5){
                    win('登录成功', Url::build('console/Shop/index'));
                }
                win('登录成功', Url::build('console/dashboard/index'));

            } else {

                fail('登录失败');

            }


        }

        // 云平台跳转
        if (empty(Cookie::has('cloud_platform'))) {
            Cookie::set('cloud_platform', 'true', 86400);
            $this->redirect('http://yun.longcai.com/yun/index.php?myurl=' . Request::instance()->domain() . Url::build('console/login/index'));
        }


        return $this->fetch('index', [

            "mate_title" => '管理员登录'

        ]);

    }


    /**
     * 检测用户是否登录
     */

    protected function checkLogin()
    {
        if (true === Session::has('manage_id')) {
            $this->redirect(Url::build('console/dashboard/index'));
        }
    }


    /**
     * 注销登录
     */

    public function logout()

    {

        Session::delete('manage_id');

        $this->redirect(Url::build('console/login/index'));

    }

}

